/**
 * (created at 2011-1-23)
 */
package qiushuo.mysqlparser.ast.expression.primary.function.string;

import java.util.List;

import qiushuo.mysqlparser.ast.expression.Expression;
import qiushuo.mysqlparser.ast.expression.primary.function.FunctionExpression;
import qiushuo.mysqlparser.generator.SQLType;

/**
 * @author <a href="mailto:QiuShuo1985@gmail.com">QIU Shuo</a>
 */
public class Char extends FunctionExpression {
    private final String charset;

    public Char(List<Expression> arguments, String charset) {
        super("CHAR", arguments);
        this.charset = charset;
    }

    @Override
    public FunctionExpression constructFunction(List<Expression> arguments) {
        throw new UnsupportedOperationException("function of char has special arguments");
    }

    @Override
    public String generateSql(SQLType type) {
        switch (type) {
        case MySQL:
            StringBuilder sb = new StringBuilder();
            sb.append("CHAR(");
            boolean isFst = true;
            for (Expression expr : arguments) {
                if (isFst) isFst = false;
                else sb.append(", ");
                sb.append(expr.generateSql(type));
            }
            if (charset != null) {
                sb.append(" USING ").append(charset);
            }
            sb.append(")");
            return sb.toString();
        default:
            throw new UnsupportedOperationException("only mysql sql can be generated");
        }
    }
}
